package com.schoolai.auth.service;

import com.alibaba.fastjson.JSON;
import com.schoolai.util.base.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;

/**
 * Copyright(C),2019-2025，XX公司
 * FileName:MyAuthenticationFailureHandler
 * Author:bobby
 * 创建时间：2025/10/21 10:06
 * Description:登录失败响应
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 */

public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
        //获取错误信息
        String localizedMessage = exception.getLocalizedMessage();
        //返回响应
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(Result.build(9999, localizedMessage)));
    }
}
